package com.exercise.widgets.menu

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.PopupMenu
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.exercise.struct.base.BaseActivity
import com.exercise.struct.viewmodel.BaseViewModel
import com.exercise.widgets.R
import com.exercise.widgets.databinding.ActivityPopMenuBinding
import com.exercise.widgets.menu.adapter.PersonAdapter
import com.exercise.widgets.menu.model.Person

class PopMenuActivity : BaseActivity<BaseViewModel, ActivityPopMenuBinding>() {

    companion object {
        fun start(context: Context) {
            Intent(context, PopMenuActivity::class.java).also {
                context.startActivity(it)
            }
        }
    }

    override val bindingInflater: (LayoutInflater) -> ActivityPopMenuBinding
        get() = ActivityPopMenuBinding::inflate
    override val modelClass: Class<BaseViewModel>
        get() = BaseViewModel::class.java


    private val mAdapter by lazy { PersonAdapter() }
    override fun initData() {
    }

    override fun initView() {
        binding.popMenu1.setOnClickListener {
            PopupMenu(this, binding.popMenu1).run {
                setOnMenuItemClickListener {
                    when (it.itemId) {
                        R.id.action_delete -> {
                            Toast.makeText(this@PopMenuActivity, "Delete", Toast.LENGTH_SHORT)
                                .show()
                            true
                        }

                        R.id.action_favorite -> {
                            Toast.makeText(this@PopMenuActivity, "Favorite", Toast.LENGTH_SHORT)
                                .show()
                            true
                        }

                        else -> false
                    }
                }
                menuInflater.inflate(R.menu.menu_pop_menu, menu)
                show()
            }
        }

        mAdapter.setOnItemClickListener { _, view, position ->
            PopupMenu(this,view).run {
                setOnMenuItemClickListener {
                    when (it.itemId) {
                        R.id.edit -> {
                            Toast.makeText(this@PopMenuActivity, "edit", Toast.LENGTH_SHORT)
                                .show()
                            true
                        }

                        R.id.delete -> {
                            Toast.makeText(this@PopMenuActivity, "delete", Toast.LENGTH_SHORT)
                                .show()
                            true
                        }

                        else -> false
                    }
                }
                menuInflater.inflate(R.menu.menu_simple_list,menu)
                show()
            }
        }
        binding.recyclerView.adapter = mAdapter
        val list = (1..10).map { Person("PopMenu:$it") }
        mAdapter.setList(list)
    }
}